package com.nd.fastdp.utils;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class DocUtil {
	
	// 格式大全:前缀对应以下方法的fmt值 
    // 0:Microsoft Word 97 - 2003 文档 (.doc) 
    // 1:Microsoft Word 97 - 2003 模板 (.dot) 
    // 2:文本文档 (.txt) 
    // 3:文本文档 (.txt) 
    // 4:文本文档 (.txt) 
    // 5:文本文档 (.txt) 
    // 6:RTF 格式 (.rtf) 
    // 7:文本文档 (.txt) 
    // 8:HTML 文档 (.htm)(带文件夹) 
    // 9:MHTML 文档 (.mht)(单文件) 
    // 10:MHTML 文档 (.mht)(单文件) 
    // 11:XML 文档 (.xml) 
    // 12:Microsoft Word 文档 (.docx) 
    // 13:Microsoft Word 启用宏的文档 (.docm) 
    // 14:Microsoft Word 模板 (.dotx) 
    // 15:Microsoft Word 启用宏的模板 (.dotm) 
    // 16:Microsoft Word 文档 (.docx) 
    // 17:PDF 文件 (.pdf) 
    // 18:XPS 文档 (.xps) 
    // 19:XML 文档 (.xml) 
    // 20:XML 文档 (.xml) 
    // 21:XML 文档 (.xml) 
    // 22:XML 文档 (.xml) 
    // 23:OpenDocument 文本 (.odt) 
    // 24:WTF 文件 (.wtf) 
	
	private static final int wdFormatPDF = 17;// PDF 格式
	
	private static final int wdFormatDoc = 16;// world 格式

	public static final int WORD_HTML = 8;
	public static final int WORD_TXT = 7;
	public static final int EXCEL_HTML = 44;
	public static final int EXCEL_XML = 46;
	public static final int EXCEL_XML_2_XLS = 1;
	public static final int EXCEL_XML_2_XLSX = 51;

	public static void wordToPDF(String source, String target) {

		ActiveXComponent app = null;
		Dispatch doc = null;
		try {
			app = new ActiveXComponent("Word.Application");
			app.setProperty("Visible", new Variant(false));
			Dispatch docs = app.getProperty("Documents").toDispatch();

			doc = Dispatch.call(docs, "Open", source).toDispatch();
			
			File tofile = new File(target);      
            if (tofile.exists()) {      
                tofile.delete();      
            }
	
			Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			Dispatch.call(doc, "Close", false);
			if (app != null)
				app.invoke("Quit", new Variant[] {});
		}
		// 结束后关闭进程
		ComThread.Release();
	}
	
	public static void xmlToDoc(String source, String target) {

		ActiveXComponent app = null;
		Dispatch doc = null;
		try {
			app = new ActiveXComponent("Word.Application");
			app.setProperty("Visible", new Variant(false));
			//app.setProperty("DefaultOpenFormat", );
			//app.setProperty("Format", new Variant("wdOpenFormatAllWord"));
			
			//Dispatch.call(Options, "Format", new Variant("wdOpenFormatAllWord"));
			
			Dispatch docs = app.getProperty("Documents").toDispatch();
			
			//doc = Dispatch.call(docs, "Open", source).toDispatch();
			doc = Dispatch.call(docs, "Open", source,  
	                new Variant(false)/* 是否进行转换ConfirmConversions */,  
	                new Variant(false),
	                new Variant(false),
	                new Variant(""),
	                new Variant(""),
	                new Variant(false),
	                new Variant(""),
	                new Variant(""),
	                new Variant("wdOpenFormatAllWord")//,
	                //new Variant("utf-8")
	                /* 是否只读 */).toDispatch();   
	
			Dispatch.call(doc, "SaveAs", target, wdFormatDoc);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			Dispatch.call(doc, "Close", false);
			if (app != null)
				app.invoke("Quit", new Variant[] {});
		}
		// 结束后关闭进程
		ComThread.Release();
	}
	
	public static void xmlToExcel(String source, String target) {

		ActiveXComponent app = null;
		Dispatch excel = null;
		try {
			app = new ActiveXComponent("Excel.Application");
			app.setProperty("Visible", new Variant(false));
			//app.setProperty("DefaultOpenFormat", );
			//app.setProperty("Format", new Variant("wdOpenFormatAllWord"));
			
			//Dispatch.call(Options, "Format", new Variant("wdOpenFormatAllWord"));
			
			app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            
            //打开要转换的Excel文件
            excel = Dispatch.call(excels, "Open", source, false, true).toDispatch();
			
            //设置兼容性检查为false
            Dispatch.put(excel, "CheckCompatibility", false);
            app.setProperty("DisplayAlerts",false);
	
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{target, new Variant(EXCEL_XML_2_XLSX)}, new int[1]);
            app.setProperty("DisplayAlerts",true);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			Dispatch.call(excel, "Close", false);
			if (app != null)
				app.invoke("Quit", new Variant[] {});
		}
		// 结束后关闭进程
		ComThread.Release();
	}

	public static void main(String[] args) {
		String source = "C:\\Users\\Administrator\\Desktop\\topci_meeting_summary.xml";
		String target = "C:\\Users\\Administrator\\Desktop\\测试.docx";
		
		xmlToDoc(source, target);
	}
}
